package com.tct.worktimeboard.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tct.worktimeboard.entity.WorkKind;
import com.tct.worktimeboard.service.IWorkKindService;
import com.tct.worktimeboard.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liyongkui
 * @since 2021-02-22
 */
@RestController
@RequestMapping("/work-kind")
public class WorkKindController {
    @Autowired
    private IWorkKindService workKindService;

    @GetMapping("/listPage")
    public JSONObject listPageWorKind(WorkKind workKind){
        return workKindService.listPageWorKind(workKind);
    }

    @PostMapping("/set")
    public JSONObject set(@RequestBody WorkKind workKind){
        //已有全局异常处理，返回结果中只需要返回正确时的返回内容即可
        int count = workKindService.count(new QueryWrapper<WorkKind>().eq("work_name", workKind.getWorkName()));
        if(count > 0){
            return JsonUtil.toErrorData("工作分类已存在");
        }
        workKindService.save(workKind);
//        if(flag){
            return JsonUtil.toJsonData();
//        }
//        return JsonUtil.toErrorData("添加失败");
    }

    @PostMapping("/edit")
    public JSONObject edit(@RequestBody WorkKind workKind){
        workKindService.updateById(workKind);
        return JsonUtil.toJsonData();
    }

    @PostMapping("/del")
    public JSONObject del(@RequestBody List<Long> ids){
        workKindService.removeByIds(ids);
        return JsonUtil.toJsonData();
    }
}
